<?php
abstract class Template
{
	private static $files = array(false);
	
	public static function reset()
	{
		self::$files = array(false);
		self::$meta = array();
		self::$vars = array();
		self::$scripts = array();
		self::$code = '';
	}
	
	public static function layout()
	{
		self::$files[] = func_get_args();
	}
	
	public static function serve($file)
	{
		$file = implode(DIRECTORY_SEPARATOR, $file);
		
		$path = APP.'view'.DIRECTORY_SEPARATOR.$file;
		
		if(file_exists($path)) return $path;
		
		return FW.'view'.DIRECTORY_SEPARATOR.$file;
	}
	
	public static function render()
	{
		$args = func_get_args();
		
		if(!current(self::$files))
		{
			self::$files[] = $args;
			self::$files[0] = array('document.html');
			
			return self::child();
		} else
		{
			return self::serve($args);
		}
	}
	
	public static function child()
	{
		$serve = self::serve(current(self::$files));
		
		next(self::$files);
		
		return $serve;
	}
	
	// Meta
	public static $meta = array();
	
	public static function meta($key, $value)
	{
		self::$meta[$key] = $value;
	}
	
	// Javascript
	public static $code = '';
	public static $vars = array();
	public static $scripts = array();
	
	public static function script()
	{
		$files = func_get_args();
		
		foreach($files as $file)
		{
			if(isset(self::$scripts[$file])) continue;
			
			if(in_array($file, fw::$scripts))
			{
				self::$scripts[$file] = WEB.'fw/js/'.$file.'.js';
			} else
			{
				self::$scripts[$file] = WEB.'js/'.$file.'.js';
			}
		}
	}
	
	public static function script_var($key, $value)
	{
		self::$vars[$key] = $value;
	}
	
	public static function script_code($code)
	{
		self::$code .= $code;
	}
}